# -*- coding: utf-8 -*-
# author: AZJ
# date: 2024/11/8
# 假如命运扼住了你的咽喉，你就去挠她的咯吱窝

import mimetypes
import smtplib
from email import encoders
from email.header import Header
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

class SendEmail:
    def __init__(self, from_addr = "595337088@qq.com", to_addr = "1332650909@qq.com",
                 smtp_server = 'smtp.qq.com', smtp_port = 465, password = "efrfirrpwgwibfee"):
        self.from_addr = from_addr
        self.to_addr = to_addr
        self.smtp_server = smtp_server
        self.smtp_port = smtp_port
        self.password = password

    def send_email(self, subject, content, attach_file = None):
        print(self.smtp_server, self.smtp_port, self.password, self.from_addr, self.to_addr)
        msg = MIMEMultipart()
        msg['From'] = Header(self.from_addr)
        msg['To'] = Header(self.to_addr)
        msg['Subject'] = Header(subject)

        msg.attach(MIMEText(content, 'plain', 'utf-8'))
        for filename in attach_file:
            content_type, encoding = mimetypes.guess_type(filename)
            if content_type is None or encoding is not None:
                content_type = 'application/octet-stream'
            part = MIMEBase(*content_type.split("/"))
            with open(filename, "rb") as fp:
                part.set_payload(fp.read())
                encoders.encode_base64(part)
                part.add_header('Content-Disposition', 'attachment', filename=filename)
                msg.attach(part)
        try:
            server = smtplib.SMTP_SSL(self.smtp_server, self.smtp_port)
            server.login(self.from_addr, self.password)
            server.sendmail(self.from_addr, self.to_addr, msg.as_string())
            print("邮件发送成功")
        except Exception as e:
            print("邮件发送失败: ", e)